package com.unlcn.ils.erpInterfaces.api.controller;

import com.alibaba.fastjson.JSON;
import com.unlcn.ils.erpInterfaces.api.vo.RestVO;
import com.unlcn.ils.erpInterfaces.api.vo.ShippingInfoVO;
import com.unlcn.ils.erpInterfaces.dao.model.LisaRequestLog;
import com.unlcn.ils.erpInterfaces.dao.model.bo.ShippingInfoBo;
import com.unlcn.ils.erpInterfaces.dao.model.bo.TrajectoryInfoBo;
import com.unlcn.ils.erpInterfaces.service.buss.ILisaRequestLogService;
import com.unlcn.ils.erpInterfaces.service.buss.ISoshipheadService;
import com.unlcn.ils.erpInterfaces.service.buss.ISoshiplineService;
import com.unlcn.ils.erpInterfaces.service.util.BaseException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping(value = "/ship", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(value = "/ship", description = "发运指令接口",tags={"发运指令接口"})
public class ShippingController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ShippingController.class);

    @Autowired
    private ISoshipheadService soshipheadService;

    @Autowired
    private ISoshiplineService soshiplineService;

    @Autowired
    private ILisaRequestLogService lisaRequestLogService;

    @GetMapping(value = "/getNewestShipByLicense")
    @ApiOperation(value = "根据车牌号获取最新发运指令信息", notes = "根据车牌号获取最新发运指令信息", produces = MediaType.APPLICATION_JSON_VALUE,response = RestVO.class)
    public RestVO<ShippingInfoBo> getNewestShipByLicense(String vclicense) throws Exception {

        RestVO<ShippingInfoBo> restVO = null;

        try {
            restVO =  new RestVO(0, soshipheadService.getNewestShip(vclicense), "success", true);
        } catch (BaseException e) {
            LOGGER.error("ShippingController.getNewestShip ERROR Message : {} ", e.getMessage());
            restVO = new RestVO<>(-1, e.getMessage(), false);
        } catch (Exception e) {
            LOGGER.error("ShippingController.getNewestShip ERROR Message : {} ", e);
            restVO = new RestVO<>(-1, e.getMessage(), false);
        }

        return restVO;
    }

    @GetMapping(value = "/getAllShipByLicense")
    @ApiOperation(value = "根据车牌号获取所有发运指令信息", notes = "根据车牌号获取所有发运指令信息", produces = MediaType.APPLICATION_JSON_VALUE,response = RestVO.class)
    public RestVO<ShippingInfoBo> getAllShipByLicense(String vclicense, String startTime, String endTime) throws Exception {

        RestVO<ShippingInfoBo> restVO = null;

        try {
            restVO =  new RestVO(0, soshipheadService.getAllShipByLicense(vclicense, startTime, endTime), "success", true);
        } catch(BaseException e) {
            LOGGER.error("ShippingController.getAllShipByLicense ERROR Message : {} ", e);
            restVO = new RestVO<>(-1, e.getMessage(), false);
        } catch (Exception e) {
            LOGGER.error("ShippingController.getAllShipByLicense ERROR Message : {} ", e);
            restVO = new RestVO<>(-1, "error", false);
        }

        return restVO;
    }

    @PostMapping(value = "/shipToByLicense")
    @ApiOperation(value = "根据车牌号和调度指令号运抵", notes = "根据车牌号和调度指令号运抵", produces = MediaType.APPLICATION_JSON_VALUE,response = RestVO.class)
    public RestVO<ShippingInfoVO> shipToByLicense(@RequestBody TrajectoryInfoBo trajectoryInfoBo) throws Exception {

        RestVO<ShippingInfoVO> restVO = null;

        LisaRequestLog lisaRequestLog = new LisaRequestLog();
        lisaRequestLog.setWay("POST");
            lisaRequestLog.setUrl("/ship/shipToByLicense");
            lisaRequestLog.setData(JSON.toJSONString(trajectoryInfoBo));
            lisaRequestLog.setGmtCreate(new Date());

            try {
            soshiplineService.shipToByLicense(trajectoryInfoBo);
            restVO =  new RestVO(0, "success", true);
            lisaRequestLog.setIsSuccess(1);
            lisaRequestLog.setFlag(1);
            lisaRequestLog.setReturnData(JSON.toJSONString(restVO));
        } catch (Exception e) {
            LOGGER.error("ShippingController.shipToByLicense ERROR Message : {} ", e);
            restVO = new RestVO<>(-1, e.getMessage(), false);
            lisaRequestLog.setIsSuccess(0);
            lisaRequestLog.setFlag(0);
            lisaRequestLog.setReturnData(JSON.toJSONString(restVO));
        }

        // 插入日志
        lisaRequestLog.setIlineid(lisaRequestLogService.getNextIlineid());
        lisaRequestLogService.insertLog(lisaRequestLog);

        return restVO;
    }

    @PostMapping(value = "/addOnWayByLicense")
    @ApiOperation(value = "根据车牌号添加在途", notes = "根据车牌号添加在途", produces = MediaType.APPLICATION_JSON_VALUE,response = RestVO.class)
    public RestVO<ShippingInfoVO> addOnWayByLicense(@RequestBody TrajectoryInfoBo trajectoryInfoBo) throws Exception {

        RestVO<ShippingInfoVO> restVO = null;

        LisaRequestLog lisaRequestLog = new LisaRequestLog();
        lisaRequestLog.setWay("POST");
        lisaRequestLog.setUrl("/ship/addOnWayByLicense");
        lisaRequestLog.setData(JSON.toJSONString(trajectoryInfoBo));
        lisaRequestLog.setGmtCreate(new Date());

        try {
            soshiplineService.addOnWayByLicense(trajectoryInfoBo);
            restVO =  new RestVO(0, "success", true);
            lisaRequestLog.setIsSuccess(1);
            lisaRequestLog.setFlag(1);
            lisaRequestLog.setReturnData(JSON.toJSONString(restVO));
        } catch (Exception e) {
            LOGGER.error("ShippingController.addOnWayByLicense ERROR Message : {} ", e);
            restVO = new RestVO<>(-1, e.getMessage(), false);
            lisaRequestLog.setIsSuccess(0);
            lisaRequestLog.setFlag(0);
            lisaRequestLog.setReturnData(JSON.toJSONString(restVO));
        }

        // 插入日志
        lisaRequestLog.setIlineid(lisaRequestLogService.getNextIlineid());
        lisaRequestLogService.insertLog(lisaRequestLog);

        return restVO;
    }
}
